<?php
error_reporting(0);
include_once '../comm/config.php';
include_once '../comm/checkpostandget.php';
include_once '../comm/class.phpmailer.php';
include_once '../comm/wjt.php';
$tag="center";
$mymsg= "邮件发送失败！可能是邮件服务器设置错误，请联系网站管理员!";
$email=trim($_POST['email']); 

$link = mysql_connect($dbserver, $dbuser, $dbpass);
if (!$link) {
     die(ERR_DB);
}
mysql_select_db($dbname);
mysql_query("set names utf8");
//检查用户名是否被注册
$q = "SELECT * FROM `".$BIAOTOU."user` where email='".$email."'";
$rs = mysql_query($q);
$total = mysql_num_rows($rs);
if($total!=1)
{
	header('Location: error.php?id=17');
	exit;
}
else
{
	while($row = mysql_fetch_object($rs)) 
	{
		$ddusername=$row->ddusername; 
		$ddpassword=$row->ddpassword; 
		$email=$row->email; 
	}
	$x = md5($ddusername.'+'.$ddpassword);
	$string = base64_encode($ddusername."@".$x);
	if(SMTPOPEN=='on'){		
			if ( ! function_exists('jmailsend'))
			{
			
				function jmailsend($from,$fromname,$to,$title,$html,$usename,$usepassword,$smtp)
				{
					$mail             = new PHPMailer();
					//$mail->SMTPDebug  = 3;
					$mail->CharSet    = "UTF-8"; // charset
					$mail->Encoding   = "base64";
					$mail->IsSMTP(); // telling the class to use SMTP

					//邮件系统配置
					$mail->SMTPAuth   = true;
					$mail->Host       = $smtp; // SMTP server
					$mail->Username   = $usename; // SMTP account username
					$mail->Password   = $usepassword;        // SMTP account password

					$mail->From       = $from;//必填，发件人Email 
					$mail->FromName   = $fromname; //必填，发件人昵称或姓名 

					$mail->WordWrap   = 50; // 自动换行的字数
					$mail->IsHTML(true); // 以 HTML发送 
					
					//主题
					$mail->Subject		= (isset($title)) ? $title : '';//必填，邮件标题（主题）

					//邮件主体
					$mail->MsgHTML($html);
					
					//发送地址
					if($to)
					{
						$address = explode("|",$to);
						foreach($address AS $key => $val)
						{
							$mail->AddAddress($val, "");
						}
					}
					global $mymsg;
					if(!$mail->Send()) {
					  $mymsg= "邮件发送失败！可能是邮件服务器设置错误，请联系网站管理员。";
					} else {
					  $mymsg= "取回密码邮件已经发送成功，请到您的邮箱收取邮件，如果没有收到邮件请与网站管理员联系。";
					}
			}			
			$html="请点击链接进入".WEBTITLE."进行密码重置。<a href=\"http://".URL."/user/getpassword_step3.php?str=".urlencode($string)."\">http://".URL."/user/getpassword_step3.php?str=".urlencode($string)."</a>";
			$from=SMTPUSER;//发送邮箱 例如：123@sina.com
			$fromname=SMTPUSER;//邮件姓名、例如张三
			$to=$email;//发到哪个邮箱，就是收信邮箱例如:456@sina.com
			$title=WEBTITLE."取回密码邮件";//邮件标题
			$usename=SMTPUSER;//登陆发送邮箱的登录名 如果$from设置为123@sina.com，则$usename=123
			$usepassword=SMTPPW;//登陆发送邮箱的密码：就是123@sina.com的登陆密码
			$smtp=SMTPHOST;//邮件服务器地址
			jmailsend($from,$fromname,$to,$title,$html,$usename,$usepassword,$smtp);
		}
	}
}     
mysql_close($link);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>找回密码 - 用户中心</title>
<link href="../css/usercss.css" rel="stylesheet" type="text/css">
<link href="../template/<?=MOBAN?>/css/common.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jquery.js"></script>
</head>
<body>
<?php
include("../template/".MOBAN."/header.tpl.php");
?>
<?php
include("../template/".MOBAN."/nav.tpl.php");
?> 
<div id="maincenter">
  <table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="50" align="left">&nbsp;</td>
  </tr>
  <tr>
    <td height="50" align="center"><img src="../images/step2.gif" alt="找回密码第二步" width="637" height="31" /></td>
  </tr>
  <tr>
    <td height="50" align="center"></td>
  </tr>
  <tr>
    <td height="50" align="center" style="font-size:14px"><?=$mymsg?></td>
  </tr>
</table>
</div>
<?php
include("../template/".MOBAN."/footer.tpl.php");
?>
</body>
</html>